package AppleCakeMining;
import java.util.ArrayList;


public class AssociationRule implements Comparable{
	private ItemSet antecedent;
	private ItemSet consequent;
	private ArrayList<Item> antecedentItems;
	private ArrayList<Item> consequentItems;
	private double interestingness;
	
	
	public AssociationRule(ItemSet ant, ItemSet con) {
		antecedent = ant;
		consequent = con;
	}

	public AssociationRule() {
		antecedentItems = new ArrayList<Item>();
		consequentItems = new ArrayList<Item>();
	}
	
	public void addToAntecedent(Item i) {
		antecedentItems.add(i);
	}
	
	public void addToConsequent(Item i) {
		consequentItems.add(i);
	}

	public void computeInterestingness(int fullSetSupport) {
		switch (Main.measure) {
		case LIFT:
			double totalTrans = GlobalTransactionList.getInstance().getTotalTransactions();
			interestingness = fullSetSupport*totalTrans/(antecedent.getSupport()*consequent.getSupport());
			//multiplication by totaltrans is due to the fact that we deal with supports, so we'd have to divide
			//the value by totalTrans once (from fullsetsupport) and multiply by it twice (for antecedent and consequent)
			break;
		case CONFIDENCE:
		default:
			interestingness = (double)(fullSetSupport)/antecedent.getSupport();
			break;
		}
		
	}
	
	public double getInterestingness() {
		return interestingness;
	}
	
	@Override
	public String toString() {
		return "Confidence= "+interestingness +" - " + antecedent.simple() + " -> " + consequent.simple();
	}

	@Override
	public int compareTo(Object obj) {
		if(!(obj instanceof AssociationRule)) return 0;
		AssociationRule toCompare = (AssociationRule)obj;
		return (int)Math.signum(interestingness - toCompare.interestingness);
	}

	public boolean isApplicable(Transaction t) {
		for(Item i:antecedent.getItems()) {
			if(!t.getItems().contains(i)) {
				return false;
			}
		}
		return true;
	}

	public ArrayList<Item> getConsequent() {
		return consequent.getItems();
	}
	
}
